package com.ruoyi.framework.interceptor;

import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 查单接口拦截
 */

@Component

public class SearchInterceptor implements HandlerInterceptor {

    public static final String TOKEN_KEY = "search_token";
    @Autowired
    private RedisCache redisCache;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String token = redisCache.getCacheObject(TOKEN_KEY);

        String searchToken = request.getHeader("search_token");

        if (searchToken == null || !searchToken.equals(token)) {

            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write("{\"code\":\"403\",\"msg\":\"未授权\"}");
            return false;
        }


        return true;

    }
}
